package com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.service.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.repository.EmergencyDisposalManageRepository;
import com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.repository.entity.EmergencyDisposalManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.emergencydisposalManage.service.EmergencyDisposalManageService;
import com.css.fxfzypg.modules.suggestinfo.manage.vo.ParamVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class EmergencyDisposalManageServiceImpl implements EmergencyDisposalManageService {
    @Autowired
    EmergencyDisposalManageRepository emergencyDisposalManageRepository;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    SUserService sUserService;

    @Override
    public Map<String, Object> save(EmergencyDisposalManageEntity emergencyDisposalManageEntity) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<>();
        String provinceId = emergencyDisposalManageEntity.getProvinceId();
        String cityId = emergencyDisposalManageEntity.getCityId();
        String areaId = emergencyDisposalManageEntity.getAreaId();
        //检验是否有重复的区县建议
        boolean res = verifySuggestion("0",emergencyDisposalManageEntity.getId(),provinceId, cityId, areaId);
        if(res==false){
            map.put("res",res);
            map.put("mess","该省市区县已存在,不能添加!");
            return map;
        }
        emergencyDisposalManageEntity.setId(UUIDGenerator.getUUID());
        emergencyDisposalManageEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(provinceId,"UTF-8")));
        emergencyDisposalManageEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(cityId,"UTF-8")));
        if(PlatformObjectUtils.isNotEmpty(areaId)){
            emergencyDisposalManageEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(areaId,"UTF-8")));

        }
        emergencyDisposalManageEntity.setDelFlag("0");
        String userId = PlatformSessionContext.getUserID();
        SUser sUser = CreateGson.createGson().fromJson(sUserService.getUserById(userId), SUser.class);
        emergencyDisposalManageEntity.setCreateId(sUser.getUserId());
        emergencyDisposalManageEntity.setCreateTime(new Date());
        boolean save = emergencyDisposalManageRepository.save(emergencyDisposalManageEntity);
        map.put("res",save);
        return map;
    }

    @Override
    public EmergencyDisposalManageEntity getDataById(String id) {
        return emergencyDisposalManageRepository.getDataById(id);
    }

    @Override
    public Map<String, Object> getDataList(ParamVo paramVo, int curPage, int pageSize) {
        return emergencyDisposalManageRepository.getDataList(paramVo,curPage,pageSize);
    }

    @Override
    public Map<String, Object> update(EmergencyDisposalManageEntity emergencyDisposalManageEntity) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashMap<>();
        //检验是否有重复的区县建议
        boolean res = verifySuggestion("1",emergencyDisposalManageEntity.getId(),emergencyDisposalManageEntity.getProvinceId(), emergencyDisposalManageEntity.getCityId(), emergencyDisposalManageEntity.getAreaId());
        if(res==false){
            map.put("res",res);
            map.put("mess","该省市区县已存在,不能修改!");
            return map;
        }
        emergencyDisposalManageEntity.setProvince(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(emergencyDisposalManageEntity.getProvinceId(),"UTF-8")));
        emergencyDisposalManageEntity.setCity(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(emergencyDisposalManageEntity.getCityId(),"UTF-8")));
        emergencyDisposalManageEntity.setArea(sysAreaService.getAreaNameByAreaId(URLEncoder.encode(emergencyDisposalManageEntity.getAreaId(),"UTF-8")));
        String userId = PlatformSessionContext.getUserID();
        SUser sUser = CreateGson.createGson().fromJson(sUserService.getUserById(userId), SUser.class);
        emergencyDisposalManageEntity.setUpdateId(sUser.getUserId());
        emergencyDisposalManageEntity.setUpdateTime(new Date());
        boolean update = emergencyDisposalManageRepository.update(emergencyDisposalManageEntity);
        map.put("res",update);
        return map;
    }

    @Override
    public boolean delete(List<String> idList) {
        boolean res = false;
        for(String id : idList){
            res = emergencyDisposalManageRepository.delete(id);
            if(!res){
                break;
            }
        }
        return res;
    }

    @Override
    public EmergencyDisposalManageEntity getEmergencyDisposalManageData(ParamsVo paramsVo) {
        EmergencyDisposalManageEntity emergencyDisposalManageData = null;
        if(paramsVo.getFlag().equals("0")){
            emergencyDisposalManageData = emergencyDisposalManageRepository.getEmergencyDisposalManageData(paramsVo);
        }
        return emergencyDisposalManageData;
    }

    @Override
    public List<EmergencyDisposalManageEntity> getEmergencyDisposalManageAreaData(ParamsVo paramsVo) {
        return emergencyDisposalManageRepository.getEmergencyDisposalManageAreaData(paramsVo);
    }

    @Override
    public EmergencyDisposalManageEntity getByProvinceCityCounty(String provinceId, String cityId, String areaId) {
        return emergencyDisposalManageRepository.getgetByProvinceCityCounty(provinceId, cityId, areaId);
    }

    /**
     * 校验区县是否存在
     * @param flag 0:保存  1:修改
     * @param provinceId
     * @param id
     * @param cityId
     * @param areaId
     * @return
     */
    private boolean verifySuggestion(String flag, String id, String provinceId, String cityId, String areaId){
        boolean res = false;
        int count = emergencyDisposalManageRepository.getCount(provinceId,cityId,areaId);
        if(flag.equals("0")){
            if(count == 0){
                res = true;
            }
        }else if(flag.equals("1")){
            if(count == 0){
                res = true;
            }else{
                String uuid = emergencyDisposalManageRepository.getId(provinceId,cityId,areaId);
                if(uuid.equals(id)){
                    res = true;
                }else{
                    res = false;
                }
            }
        }
        return res;
    }
}
